#ifndef STATEMANAGER_H
#define STATEMANAGER_H

#include <QObject>
#include <QMap>
#include <QDateTime>

// 系统状态类型
enum class StateType {
    Unknown = 0,
    Normal,
    Initializing,
    Timeout,
    Error,
    Critical
};

// 状态信息结构体
struct StateInfo {
    StateType type = StateType::Unknown;
    QString description;
    QDateTime timestamp;

    StateInfo() = default;
    StateInfo(StateType t, QString desc)
        : type(t),
          description(std::move(desc)),
          timestamp(QDateTime::currentDateTime()) {}
};

// 状态管理类
class StateManager : public QObject {
    Q_OBJECT
public:
    static StateManager& instance();

    // 设置或更新状态
    void setState(const QString& key, StateType type, const QString& description);

    // 获取状态
    StateInfo getState(const QString& key) const;

    // 清除状态
    void clearState(const QString& key);

    // 获取全部状态
    QMap<QString, StateInfo> allStates() const;

    // 打印所有状态
    void printAllStates() const;

signals:
    void stateChanged(const QString& key, const StateInfo& info);
    void stateCleared(const QString& key);

private:
    explicit StateManager(QObject* parent = nullptr);
    QMap<QString, StateInfo> m_states;
};



/*
void testPrintAllStates() {
    auto& sm = StateManager::instance();

    sm.setState("joint_1", StateType::Normal, "运行正常");
    sm.setState("joint_2", StateType::Timeout, "通信超时");
    sm.setState("controller", StateType::Error, "控制器失联");

    sm.printAllStates(); //  打印所有状态

    sm.clearState("joint_2");
    sm.printAllStates(); // 再次打印，验证更新
}
*/


#endif // STATEMANAGER_H
